package com.pug.service.hotel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pug.bo.HotelCommentBo;
import com.pug.mapper.hotel.HotelCommentMapper;
import com.pug.mapper.hotel.HotelZanMapper;
import com.pug.pojo.HotelComment;
import com.pug.pojo.KssUser;
import com.pug.service.redis.PugRedisOperator;
import com.pug.service.user.UserServiceImpl;
import com.pug.vo.HotelCommentPageVo;
import com.pug.vo.HotelCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/3/19$ 16:58$
 */
@Service
@Slf4j
public class HotelCommentServiceImpl extends ServiceImpl<HotelCommentMapper, HotelComment> implements IHotelCommentService{
    @Resource
    HotelZanMapper zanMapper;
    @Resource
    HotelCommentMapper hotelCommentMapper;
    @Autowired
    private PugRedisOperator redisOperator;
    @Resource(name = "stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate;
//    @Autowired
//    private IHotelommentUserZanServiceImpl hotelommentUserZanService;
    @Resource
    UserServiceImpl userService;
    @Override
    public HotelCommentBo saveComment(HotelCommentVo hotelCommentVo, KssUser kssUser) {
        Long userId=kssUser.getId();
        String nickName=kssUser.getNickname();
        String avatar=kssUser.getAvatar();
        HotelComment hotelComment = new HotelComment();
        hotelComment.setUserid(userId);
        hotelComment.setUsernickname(nickName);
        hotelComment.setUseravatar(avatar);
        hotelComment.setContent(hotelCommentVo.getContent());
        hotelComment.setParentId(hotelCommentVo.getParentId());
        hotelComment.setHotelId(hotelCommentVo.getHotelId());
        //保存本条评论
        this.baseMapper.insert(hotelComment);
        return tranferBo(hotelComment,HotelCommentBo.class);
    }
    /**
     * 保存酒店评论
     *
     * @param hotelCommentPageVo
     * @return
     */
    public Page<HotelCommentBo> loadHotelComment(HotelCommentPageVo hotelCommentPageVo) {
        //1: 查询第一级的评论
        Page<HotelCommentBo> hotelCommentBoPage = loadCommentChildren(hotelCommentPageVo.getHotelId(),
                hotelCommentPageVo.getParentId(), hotelCommentPageVo.getPageNo(), hotelCommentPageVo.getPageSize());
        //2：处理子集的评论
        List<HotelCommentBo> hotelCommentBoList = hotelCommentBoPage.getRecords();
        //存在评论在查询对应的子评论
        if(hotelCommentBoList.size()>0){
            List<HotelCommentBo> collect = hotelCommentBoList.stream().peek(new Consumer<HotelCommentBo>() {
                @Override
                public void accept(HotelCommentBo hotel) {
                    Page<HotelCommentBo> hotelCommentBoPage1 = loadCommentChildren(hotel.getHotelId(), hotel.getId(), 1, 3);
                    hotel.setPageChildrens(hotelCommentBoPage1);
//                    long zanNums=stringRedisTemplate.opsForHash().size(hotel.getId()+"");
                    //在数据库中查询啦
                    int total = zanMapper.total(hotel.getId());
                    //在redis中查询
                    long size = stringRedisTemplate.opsForHash().size(hotel.getId() + "");
                    hotel.setZannum((int)(total+size));
                }
            }).collect(Collectors.toList());
            hotelCommentBoPage.setRecords(collect);
        }
        // 4:返回
        return hotelCommentBoPage;
    }
    /**
     * 查询酒店分页
     * @param hotelId
     * @param parentId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<HotelCommentBo> loadCommentChildren(Long hotelId, Long parentId, Integer pageNo, Integer pageSize) {
        Page<HotelComment> page = new Page<>(pageNo,pageSize);
        QueryWrapper<HotelComment> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId);
        wrapper.eq("parent_id",parentId);
        wrapper.orderByDesc("create_time");
        Page<HotelComment> page1 = this.page(page, wrapper);
        //查询评论点赞数
        List<HotelComment> records = page1.getRecords();
        for(HotelComment a:records){

        }
        return tranferPageBo(page1,HotelCommentBo.class);
    }
    //删除评论
    public boolean deleteComment(Long userId,Long commentId){
        QueryWrapper<HotelComment> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userId);
        wrapper.eq("id",commentId);
        return this.baseMapper.delete(wrapper)==1;
    }
}
